package cfca.seal.mananger;

import cfca.seal.bean.Seal;
import cfca.seal.bean.SealCert;
import cfca.seal.common.LRUCache;
import cfca.seal.dao.main.ISealCertDao;
import cfca.seal.dao.main.ISealDao;
import cfca.seal.factory.PrePdfSealFactory;
import cfca.seal.maker.util.SealExtracter;
import cfca.seal.sadk.PrePdfSeal;
import cfca.seal.util.EncryptUtil;
import java.util.List;
import javax.servlet.ServletContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class PdfSealManager
{
  private static final Logger log = LoggerFactory.getLogger("web_sys");
  static final int cacheSize = 200;
  private static LRUCache<String, PrePdfSeal> pdfSealMap = new LRUCache(200);
  private static PdfSealManager instance = null;
  ISealDao dao;
  ISealCertDao certdao;

  public static PdfSealManager getInstance()
  {
    if (instance == null) {
      instance = new PdfSealManager();
    }
    return instance;
  }

  public void init(ServletContext servletContext)
  {
    synchronized (this) {
      WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
      this.dao = ((ISealDao)wac.getBean(ISealDao.class));
      this.certdao = ((ISealCertDao)wac.getBean(ISealCertDao.class));
      try {
        int sealSize = 0;
        List list = this.dao.getByCount(200);
        if (list != null) {
          for (int i = 0; (i < list.size()) && 
            (i != 200); i++)
          {
            Seal seal = (Seal)list.get(i);
            if ((1 == seal.getStatus()) && (-1 != seal.getCertType())) {
              sealSize++;
              SealManager.getInstance().addSeal(seal);
              PrePdfSeal prePdfSeal = createPdfSeal(seal);
              pdfSealMap.put(seal.getCode(), prePdfSeal);
            }
          }
        }
        log.info("印章管理容器初始始化成功:" + sealSize);
      } catch (Exception e) {
        log.error("印章管理容器初始始化失败", e);
      }
    }
  }

  private PrePdfSeal createPdfSeal(Seal sealBean) throws Exception {
    PrePdfSeal prePdfSeal = null;
    if (sealBean.getCertType() == 0) {
      SealExtracter sEx = new SealExtracter(sealBean.getSealData(), EncryptUtil.decrypto(sealBean.getSealPfxPwd()));
      int keyType = sEx.getKeyType();
      if (1 == keyType)
        prePdfSeal = PrePdfSealFactory.generatePrePdfSeal(sealBean);
      else if (2 == keyType)
        prePdfSeal = PrePdfSealFactory.generatePrePdfSealSm2(sealBean);
    }
    else if ((sealBean.getCertType() == 1) || (sealBean.getCertType() == 2)) {
      SealCert cert = this.certdao.getSealCertById(sealBean.getSealCertId());
      prePdfSeal = PrePdfSealFactory.generatePrePdfSeal(sealBean, cert);
    }
    return prePdfSeal;
  }

  public PrePdfSeal getSealByCode(String sealCode) throws Exception {
    PrePdfSeal pdfSeal = (PrePdfSeal)pdfSealMap.get(sealCode);
    if (pdfSeal != null) {
      return pdfSeal;
    }
    Seal seal = this.dao.getSealByCode(sealCode);
    if (seal != null) {
      PrePdfSeal prePdfSeal = createPdfSeal(seal);
      if (prePdfSeal == null)
        return null;
      pdfSealMap.put(seal.getCode(), prePdfSeal);
      log.info("印章管理容器增加新的印章:" + seal.getCode());
      return prePdfSeal;
    }

    return null;
  }

  public void addPdfSeal(Seal seal) {
    synchronized (this) {
      try {
        PrePdfSeal prePdfSeal = createPdfSeal(seal);
        if (prePdfSeal == null)
          return;
        pdfSealMap.put(seal.getCode(), prePdfSeal);
        log.info("印章管理容器增加新的印章:" + seal.getCode());
      } catch (Exception e) {
        log.error("印章管理容器初始始化失败", e);
      }
    }
  }

  public void removePdfSeal(String sealCode) {
    synchronized (this) {
      pdfSealMap.remove(sealCode);
      log.info("印章管理容器初始化删除印章:" + sealCode);
    }
  }
}